Concatenation
This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this ''Introduction To ChoiceScript ''series of linked articles: A Basic Tutorial. To concatenate - in programming - is to join or combine two or more string (or 'text') values together, and it is a particuarly useful operation to use when writing interactive fiction. You can concatenate both new, raw string values or those already held within variables. You can see an example of how it might be used to the right. The basics (full explanation) Basic concatenation is easy enough and requires only that you use one operator '&' and a command you should already be familar with: *set But before we explain it, let's take a look at a few examples of where it might be useful. Let's say your game allows the player to name their character's first and last name and at a certain point in the story, you wish them to be addressed by both. Without concatenation we'd have to do something like this: Hail! ${firstname} ${lastname}, what tidings do you bring? Easy yes? Now let's say you wanted a title (Mr / Mrs) prefix as well: Hail! ${title} ${firstname} ${lastname}, what tidings do you bring? As you can see, if you had to write all this several times throughout your novel, it would begin to get pretty tedious and your chances of a typo (and a related error) would increase dramatically. So how can concatenation help? All the above variables contain strings, or text. We can combine these into one variable via concatenation and subsequently print our player character's full name with just one variable, like ${fullname}. Here's a quick example: *set fullname "May"&"Fair" Now whenever you print the variable fullname, the text "MayFair" will appear. Of course, there's a problem with this. When you concatenate strings, they are joined end-to-end, it doesn't add spaces. We need to do that ourselves like so: *set fullname "May "&"Fair" Printing the fullname variable will now show the text: "May Fair" Which, going back to our previous example will allow us to change this: Hail! ${firstname} ${lastname}, what tidings do you bring? to this: Hail! ${fullname}, what tidings do you bring? Which is much simpler, no? Further concatenation (brief notes) Once you believe you've got a firm grasp on the above section, you can find additional information about methods of concatenation within choicescript below. Concatenating More Than Two Strings Concatenating more than two strings requires the use of parentheses, to group the arguments into groups of two: *set fullname ("Mr "&"John ")&"Smith" - is the correct syntax *set fullname "Mr "&"John "&"Smith" - will NOT '''work This pattern must continue, but there are no reasonable limits to how many string values you can concatenate: *set fullname ("Mr "&"John ")&"Smith"&" Barren" - will '''NOT '''work *set fullname ("Mr "&"John ")&("Smith"&" Barren") - is the correct syntax Think of it as being only to concatenate in groups of two, pairs within brackets will be combined first and then each concatenated bracket will be concatenated with each other: *set fruitlist (("Apples, "&"pears,")&(" oranges,"&" lemons,"))&((" grapefruits,"&" melons,")&(" bananas"&" and a carrot")) The above should work, in theory and would make ${fruitlist} print: "Apples, pears, oranges, lemons, grapefruits, melons, bananas and a carrot". However it is better practice to perform several small, seperate concatenations and combine them later, to avoid long, hard to read code, like the above. Concatenating current variables To concatenate values within variables you follow the same syntax as before, but do not enclose your arguments in quotation marks, for example: *set firstname "Roger" *set lastname "Davis" *set fullname (firstname&" ")&lastname Note: the seperate space string, this is because in a firstname/lastname variable we wouldn't have a space and cannot add it like we could with raw strings. ${fullname} would now print: "Roger Davis" '''Next Tutorial Article: Publishing your game Related articles **set *Variable types **gotoref **setref Category:Tutorials